package com.yeyks.app.controller.base.test;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yeyks.commonReference.service.consumer.MemberCardService;
import com.yeyks.commonReference.service.consumer.UserMemberCardService;
import com.yeyks.commonReference.service.vip.MemberCardOnlineService;
import com.yeyks.consumer.dal.domain.MemberCard;
import com.yeyks.consumer.dal.domain.UserInfo;
import com.yeyks.consumer.dal.domain.UserMemberCard;
import com.yeyks.consumer.param.UserMemberCard.UserMemberCardBindCardParam;
import com.yeyks.consumer.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Objects;

@Profile("test")
@Component
@Slf4j
public class SomeThingDo {

    @Autowired
    private SomeThingDo someThingDo;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserMemberCardService userMemberCardService;
    @Autowired
    private MemberCardService memberCardService;
    @Autowired
    private MemberCardOnlineService memberCardOnlineService;

    @PostConstruct
    public void init() {
//        根据列表开通推手();
    }

    public void 根据列表开通推手() {
        log.error("根据列表开通推手==============================================================start");
        List<String> phoneList = CollectionUtil.newArrayList(
                "",
                "",
                "",
                "",
                "",
                ""
        );
        if (CollectionUtil.isNotEmpty(phoneList)) {
            for (String phone : phoneList) {
                if (StrUtil.isNotBlank(phone)) {
                    try {
                        someThingDo.开通推手(phone);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        log.error("根据列表开通推手==============================================================end");
    }

    @Transactional(rollbackFor = Exception.class)
    public void 开通推手(String phone) {
        Integer cardType = 2;
        UserInfo userInfo = userInfoService.selectUseInfoPhone(phone);
        //是否有卡
        UserMemberCard userMemberCard = userMemberCardService.getOne(
                new QueryWrapper<UserMemberCard>()
                        .eq(UserMemberCard.USER_ID, userInfo.getId())
                        .last("limit 1")
        );
        if (Objects.isNull(userMemberCard)) {
            //抽卡
            MemberCard memberCard = memberCardOnlineService.getEnableCard(cardType);
            //改卡状态
            memberCardService.update(
                    new UpdateWrapper<MemberCard>()
                            .eq(MemberCard.ID, memberCard.getId())
                            .set(MemberCard.ENABLE, 1)
            );
            //绑卡
            UserMemberCardBindCardParam userMemberCardBindCardParam = new UserMemberCardBindCardParam();
            userMemberCardBindCardParam.setUserId(userInfo.getId());
            userMemberCardBindCardParam.setCardType(cardType);
            userMemberCardBindCardParam.setCardNo(memberCard.getCardNo());
            userMemberCardBindCardParam.setPassword(memberCard.getPassword());
            userMemberCardBindCardParam.setPayPassword(null);
            userMemberCardBindCardParam.setInviteId(null);
            userMemberCardService.userBindCard(userMemberCardBindCardParam);
            //更新卡余额
            boolean b = userMemberCardService.update(
                    new UpdateWrapper<UserMemberCard>()
                            .set(UserMemberCard.TOTAL_GIVE_AMOUNT, 0)
                            .set(UserMemberCard.REMAIN_GIVE_AMOUNT, 0)
                            .eq(UserMemberCard.USER_ID, userInfo.getId())
                            .eq(UserMemberCard.CARD_TYPE, cardType)
            );
        }
    }

}
